package org.je.dev.image;

/**
 * @author fanyifeng
 */
public class SimpleFilter
{
	static byte	full	= (byte)255;

	/**
	 * @param img
	 * @param num
	 */
	public static void deleteLower(FastImage img, int num) {
		byte[] alphas = img.alphas();
		byte[] rgbs = img.rgbs();

		int aLen = alphas.length;

		int aPointer = 0;
		int rgbPointer = 0;

		for (; aPointer < aLen; ++aPointer) {
			int r = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			int g = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			int b = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			if (r < num && g < num && b < num) {
				alphas[aPointer] = 0;
			} else {
				alphas[aPointer] = full;
			}
		}
	}

	/**
	 * @param img
	 * @param num
	 */
	public static void deleteUpper(FastImage img, int num) {
		byte[] alphas = img.alphas();
		byte[] rgbs = img.rgbs();

		int aLen = alphas.length;

		int aPointer = 0;
		int rgbPointer = 0;

		for (; aPointer < aLen; ++aPointer) {
			int r = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			int g = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			int b = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			if (r > num && g > num && b > num) {
				alphas[aPointer] = 0;
			} else {
				alphas[aPointer] = full;
			}
		}
	}

	/**
	 * @param img
	 * @param rgb
	 */
	public static void deleteRGB(FastImage img, int rgb) {
		int R = (rgb >> 16) & 0xff;
		int G = (rgb >> 8) & 0xff;
		int B = rgb & 0xff;

		byte[] alphas = img.alphas();
		byte[] rgbs = img.rgbs();

		int aLen = alphas.length;

		int aPointer = 0;
		int rgbPointer = 0;

		for (; aPointer < aLen; ++aPointer) {
			int r = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			int g = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			int b = rgbs[rgbPointer] & 0xff;
			++rgbPointer;
			if (r == R && g == G && b == B) {
				alphas[aPointer] = 0;
			} else {
				alphas[aPointer] = full;
			}
		}
	}
}
